//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_OS_CENV_H_
#define _BUOLA_OS_CENV_H_

#include <buola/buola.h>
#include <map>

namespace buola {

///\addtogroup os
///@{

///////////////////////////////////////////////////////////////////////////

/// environment variables interface

/// Provides a portable interface to the operating-system functions
/// that get and set environment variables
///
///////////////////////////////////////////////////////////////////////////

class CEnv
{
public:
    ///get a variable

    ///Returns the value of variable \p pName. If the variable
    ///is not set, it returns \p pDefault, and if \p pSet is true, sets the
    ///variable to that value.
    static std::string Get(const std::string &pName,const std::string &pDefault
                    =std::string(),bool pSet=false);

    ///returns true if the variable is set
    static bool IsSet(const std::string &pName);

    ///set a variable

    ///Sets the variable pName to have the value pValue. If the variable is
    ///already set, it is only overwritten if pOver is true
    static void Set(const std::string &pName,const std::string &pValue,
                                                    bool pOver=true);
    ///unset a variable
    static void Unset(const std::string &pName);

    ///returns a map with all environment variables
    static void GetAll(std::map<std::string,std::string> &pMap);
};

///////////////////////////////////////////////////////////////////////////

/// temporal change to environment variable

/// This class saves the current value of an environment variable and modifies
/// its value in the constructor. The destructor restores the previous value.
/// It is useful to call a certain system function with a modified environment.
///
///////////////////////////////////////////////////////////////////////////

class CEnvChange
{
public:
    ///starts the variable change
    CEnvChange(const std::string &pName,const std::string &pValue);

    ///restores the previous value of the variable
    ~CEnvChange();

private:
    std::string mName;
    bool mSet;
    std::string mValue;
};

///@}

/*namespace buola*/ }

#endif
